package cn.edu.neusoft.neufooddemo.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.edu.neusoft.neufooddemo.R;
import cn.edu.neusoft.neufooddemo.activity.MyCommentActivity;
import cn.edu.neusoft.neufooddemo.bean.OrderInfo;
import cn.edu.neusoft.neufooddemo.bean.Result;
import cn.edu.neusoft.neufooddemo.listener.RetrofitListener;
import cn.edu.neusoft.neufooddemo.model.FoodModel;

/**
 * Created by Shinelon on 2017/3/31.
 */

public class OrderAdapter extends RecyclerViewAdapter<OrderAdapter.ViewHolder> implements RetrofitListener<Result>{

    private FoodModel foodModel;

    public OrderAdapter(Context context, List data, int layoutResource) {
        super(context, data, layoutResource);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final OrderInfo entity = (OrderInfo) data.get(position);
        holder.myorder_orderid.setText("订单号："+entity.getOrder_id());
        holder.myorder_itemid.setText(entity.getItem_id());
        holder.myorder_shopname.setText(entity.getShopname());
        holder.myorder_ordertime.setText(entity.getOrder_time());
        holder.myorder_foodname.setText(entity.getFoodname());
        holder.myorder_sum.setText("￥" + entity.getSum());
        if(entity.getContent()!=null){
            holder.myorder_comment.setVisibility(View.GONE);
        }
        holder.myorder_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final EditText et = new EditText(context);
                            new AlertDialog.Builder(context).setTitle("评论")
                                    .setView(et)
                                    .setPositiveButton("确定",new DialogInterface.OnClickListener(){
                                        public void onClick(DialogInterface dialog, int which) {
                                            doComment(entity.getItem_id(),et.getText().toString());
                                        }
                                    }).setNegativeButton("取消",null)
                                    .show();
            }
        });
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView myorder_shopname, myorder_ordertime, myorder_foodname, myorder_sum, myorder_orderid,myorder_itemid;
        Button myorder_comment;

        public ViewHolder(View itemView) {
            super(itemView);
            myorder_orderid = (TextView) itemView.findViewById(R.id.myorder_orderid);
            myorder_itemid = (TextView) itemView.findViewById(R.id.myorder_itemid);
            myorder_shopname = (TextView) itemView.findViewById(R.id.myorder_shopname);
            myorder_ordertime = (TextView) itemView.findViewById(R.id.myorder_ordertime);
            myorder_foodname = (TextView) itemView.findViewById(R.id.mycart_foodname);
            myorder_sum = (TextView) itemView.findViewById(R.id.myorder_sum);
            myorder_comment = (Button) itemView.findViewById(R.id.myorder_comment);
        }

    }

    private void doComment(String item_id,String et){
        foodModel=new FoodModel();
        foodModel.insertComment(item_id,et,this);
    }

    @Override
    public void onSuccess(Result result, int flag) {
        if(result.getSuccess().equals("1")){
            Intent intent=new Intent(context,MyCommentActivity.class);
            context.startActivity(intent);
            Toast.makeText(context,"评论成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(context,"评论失败", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFail() {
        Toast.makeText(context,"网络错误", Toast.LENGTH_SHORT).show();
    }
}